package com.fuze.navi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fuze.navi.common.Advice;
import com.fuze.navi.common.NaviException;
import com.fuze.navi.domain.dto.InsertFormDTO;
import com.fuze.navi.domain.model.Coordinate;
import com.fuze.navi.domain.model.Line;
import com.fuze.navi.domain.model.Station;
import com.fuze.navi.domain.vo.CoordinateParamVO;
import com.fuze.navi.mapper.CoordinateMapper;
import com.fuze.navi.mapper.LineMapper;
import com.fuze.navi.mapper.StationMapper;
import com.fuze.navi.service.NavigationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @author sepc_
 * @date 3/19/2021 09:30
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class NavigationServiceImpl implements NavigationService {

    final CoordinateMapper coordinateMapper;

    final StationMapper stationMapper;

    final LineMapper lineMapper;

    @Value("amap.key")
    String amapKey;

    @Override
    public List<Station> listStation(String voltageLevel) {
        QueryWrapper<Station> qw = new QueryWrapper<>();
        qw.eq(Station.COL_VOLTAGE_LEVEL, voltageLevel);
        return stationMapper.selectList(qw);
    }

    @Override
    public List<Coordinate> listCoordinate(Long lineId) {
        QueryWrapper<Coordinate> qw = new QueryWrapper<>();
        qw.eq(Coordinate.COL_LINE_ID, lineId);
        return coordinateMapper.selectList(qw);
    }

    @Override
    public void deleteByCoordinateId(Long[] coordinateIds) {
        List<Long> asList = Arrays.asList(coordinateIds);
        if (asList.size() <= 0) {
            throw new NaviException(Advice.FAILED_DELETED);
        }
        this.coordinateMapper.deleteBatchIds(asList);
    }

    @Override
    public void update(Coordinate coordinate) {
        Long id = coordinate.getId();
        if (id == null) {
            throw new NaviException(Advice.FAILED_VERIFICATION);
        }
        int i = this.coordinateMapper.updateById(coordinate);
        if (i != 1) {
            throw new NaviException(Advice.FAILED_UPDATED);
        }
    }

    @Override
    public void insert(InsertFormDTO insertFormDTO) {
        int res = 0;
        Station station = new Station();
        station.setStationName(insertFormDTO.getStationName());
        station.setVoltageLevel(insertFormDTO.getVoltageLevel());
        res += this.stationMapper.insert(station);
        Long stationId = station.getId();
        Line line = new Line();
        line.setLineName(insertFormDTO.getLineName());
        line.setStationId(stationId);
        res += this.lineMapper.insert(line);
        Long lineId = line.getId();
        Coordinate coordinate = new Coordinate();
        coordinate.setDeviceId("");
        coordinate.setDeviceName(insertFormDTO.getDeviceName());
        coordinate.setVoltageLevel(insertFormDTO.getVoltageLevel());
        coordinate.setLon(insertFormDTO.getLon());
        coordinate.setLat(insertFormDTO.getLat());
        coordinate.setLineId(lineId);
        res += this.coordinateMapper.insert(coordinate);
        if (res != 3) {
            throw new NaviException(Advice.FAILED_INSERTED);
        }
    }

    @Override
    public void navigate(CoordinateParamVO coordinateParamVO) {
    }

    @Override
    public List<Line> listLine(Long stationId) {
        QueryWrapper<Line> qw = new QueryWrapper<>();
        qw.eq(Line.COL_STATION_ID, stationId);
        return lineMapper.selectList(qw);
    }

    @Override
    public Set<String> listAllVoltage() {
        return stationMapper.listVoltageLevel();
    }
}
